﻿using System;

using Bronze.XXTong.DAL.DatabaseSpecific;
using Bronze.XXTong.DAL.Linq;
using Bronze.XXTong.DAL.EntityClasses;

namespace Bronze.XXT.BLL
{
    /// <summary>
    /// 关于话机日志的操作
    /// </summary>
    public class PhoneMonitorLog
    {
        /// <summary>
        /// 话机连接状态发生改变时保存日志
        /// </summary>
        /// <param name="phoneID"></param>
        /// <param name="strPhoneSerialNumber">设备编号</param>
        /// <param name="intConnectStatus">连接状态  1.已连接  2.未连接  3.连接异常</param>
        /// <param name="intShellStatus">外壳门状态</param>
        /// <param name="strPowerSupplyStatus">供电状态</param>
        /// <param name="strPhoneSystemVersion">公话设备版本</param>
        public static void SaveLog(Guid phoneID,
            string strPhoneSerialNumber,
            int intConnectStatus,
            Nullable<int> intShellStatus = null,
            string strPowerSupplyStatus = null,
            string strPhoneSystemVersion = null)
        {
            //bool isNull = adapter == null;
            //if (isNull)
            //    adapter = new DataAccessAdapter();
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                LinqMetaData metaData = new LinqMetaData(adapter);

                PublicPhoneMonitorLogEntity entity = CreateLogEntity(phoneID,
                    strPhoneSerialNumber,
                    intConnectStatus,
                    intShellStatus,
                    strPowerSupplyStatus,
                    strPhoneSystemVersion);

                adapter.SaveEntity(entity);
            }
            //if (isNull)
            //{
            //    adapter.CloseConnection();
            //    adapter.Dispose();
            //}

        }

        public static PublicPhoneMonitorLogEntity CreateLogEntity(Guid phoneID,
            string strPhoneSerialNumber,
            int intConnectStatus,
            Nullable<int> intShellStatus = null,
            string strPowerSupplyStatus = null,
            string strPhoneSystemVersion = null) 
        {
            PublicPhoneMonitorLogEntity entity = new PublicPhoneMonitorLogEntity();
            entity.FId = Guid.NewGuid();
            entity.FPhoneId = phoneID;
            entity.FPhoneSerialNumber = strPhoneSerialNumber;
            entity.FConnectStatus = intConnectStatus;
            entity.FConnecteTime = DateTime.Now;
            entity.FShellStatus = intShellStatus;
            entity.FPowerSupplyStatus = strPowerSupplyStatus;
            entity.FPhoneSystemVersion = strPhoneSystemVersion;
            return entity;
        }
    }
}
